<?php

namespace App\Http\Controllers\Api\Customer;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Models\Goods\GoodsComment;

use App\Models\Customer\Customer;  //会员主表

class CustomerCommentController extends Controller
{
    /**
     * 获取商品评论初始数据
     * @param Request $request
     * @return array
     */
    public function goodsComment(Request $request)
    {

        $mainPostID = $request->input('mid', 0);
        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);
        $grade = $request->input('grade', 0);
        $rp = $request->input('rp', 5);

        $pcustID = 0;
        if ($session_id) {
            $pcustID = get_customer_id($session_id, $platform);
        }

        $sql = [];
        if ( $grade == 1 ) {
            $sql = [
                ['commentLevel', '>=', 4],
                ['commentLevel', '<=', 5],
            ];
        } else if ($grade == 2) {
            $sql = [
                ['commentLevel', '>=', 2],
                ['commentLevel', '<=', 3],
            ];
        } else if ($grade == 3) {
            $sql = ['commentLevel' => 1];
        }

        $sql_str = [];
        if (ebsig_is_int($pcustID)) {
            $sql_str = ['display' => 0, 'pcustID' => $pcustID];
        }

        $comment = [];  //评论信息
        $comment_data = GoodsComment::where(['commentStatus' => 1, 'mainPostID' => $mainPostID])
            ->where($sql)
            ->where(['display' => 1])
            ->orWhere($sql_str)
            ->orderBy('commentsId', 'DESC')
            ->paginate($rp)
            ->toArray();
        if ($comment_data['data']) {
            foreach ($comment_data['data'] as &$c) {

                //查询会员信息
                $cust = Customer::find($c['pcustID']);
                if (!$cust) {
                  continue;
                }

                $comment[] = [
                    'cust_name' => !empty($cust['custname']) ? $cust['custname'] : $cust['custID'],
                    'commentsTime' => date('Y-m-d',strtotime($c['createTime'])),
                    'commentLevel' => $c['commentLevel'],
                    'info' => $c['info'],
                    'answerInfo' => $c['answerInfo']
                ];
            }
        }

        return response()->json(['code' => 200, 'message' => 'ok', 'data' => [
            'count' => $comment_data['total'],
            'list' => $comment
        ]]);

    }

}